<?php if (!defined('FIT_PATH')) exit('No direct script access allowed');
/**
 * @copyright 2009 - 2011 Bincent.com. All Rights Reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @package FitPHP
 * @subpackage core
 * 
 * @author Hongbin Hsu <hongbin.hsu@gmail.com> Initial.
 * @version $Id: core/common.php 1.0 2011-01-03 00:05 hongbin.hsu $
 */

//----------------------------------------------------------
/**
 * 自动加载项目文件
 *
 * @param string $class 对象名称
 */
function __autoload($class)
{
	$class = strtolower($class);
    $class = str_replace('_', DIRECTORY_SEPARATOR, $class);
    autoload($class, false);
}

//----------------------------------------------------------
/**
 * 加载类函数
 *
 * @param string $filename	文件路径及名称
 * @param bool $instantiate 是否实例化
 * @return mixed
 */
function &autoload($filename, $instantiate = true, $params = null)
{
	static $objects = array();
	
	$class = format_string(str_replace('/', '_', $filename));
	$filename = str_replace('/', DIRECTORY_SEPARATOR, $filename);
	
	if (isset($objects[$class]))
	{
		return $objects[$class];
	}
	
	if (file_exists(APP_PATH.$filename.'.php'))
	{
		include_once(APP_PATH.$filename.'.php');
	}else{
		require_once(FIT_PATH.$filename.'.php');
	}
	
	if ($instantiate == false)
	{
		$objects[$class] = true;
		return $objects[$class];
	}else{
        $objects[$class] = &instantiate_class(new $class($params));
        return $objects[$class];
    }
}

//----------------------------------------------------------
/**
 * 实例化类
 *
 * @param mixed $class_object
 * @return mixed
 */
function &instantiate_class(&$class_object)
{
	return $class_object;
}

//----------------------------------------------------------
/**
 * 格式化字符串 [格式:One_Two]
 *
 * @param string $class
 * @return string
 */
function format_string($string)
{
	$string = ucfirst(strtolower($string));
	return preg_replace('/_(.)/e', "'_'.ucfirst('\\1')", $string);
}
